﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace NewCROStarter
{
    public partial class AI_small_window : Form
    {
        public AI_small_window(Process prog, string Code)
        {
            curprocess = prog;
            ActivationCode = Code;
            InitializeComponent();
        }

        private Process curprocess;
        string ActivationCode = "";

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool ReadProcessMemory(
          IntPtr hProcess,
          IntPtr lpBaseAddress,
          [Out] byte[] lpBuffer,
          int dwSize,
          out int lpNumberOfBytesRead
         );
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool WriteProcessMemory(
            IntPtr hProcess,
            IntPtr lpBaseAddress,
            byte[] lpBuffer,
            uint nSize,
            out int lpNumberOfBytesWritten);

        private void AI_small_window_Load(object sender, EventArgs e)
        {
            ReposAISmallWindow();
            timer1.Enabled = true;
            timer1.Start();
        }

        private void ReposAISmallWindow()
        {
            Rectangle rect = Screen.GetWorkingArea(this);
            this.Location = new Point(0, rect.Height-this.Height);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            timer1.Enabled = false;
            try
            {
                int baseaddr = curprocess.MainModule.BaseAddress.ToInt32();
                int offset1 = 0x005B36D8;
                IntPtr finaladdr = (IntPtr)(baseaddr + offset1);
                byte[] buffer = new byte[32];
                int bytesread;
                bool ok = ReadProcessMemory(curprocess.Handle, finaladdr, buffer, 32, out bytesread);
                lbl_AI_name.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(buffer);

                int offset2 = 0x005B3740;
                finaladdr = (IntPtr)(baseaddr + offset2);
                buffer = new byte[1];
                ok = ReadProcessMemory(curprocess.Handle, finaladdr, buffer, 1, out bytesread);
                lbl_AI_eat.Text = buffer[0].ToString();
                if (buffer[0] <= 24)
                {
                    lbl_AI_eat.ForeColor = Color.Red;
                    if (panel1.BackColor == Color.Silver)
                    {
                        lbl_AI_eat.BackColor = Color.Yellow;
                        lbl_AI_love.BackColor = Color.Yellow;
                        lbl_AI_name.BackColor = Color.Yellow;
                        label2.BackColor = Color.Yellow;
                        panel1.BackColor = Color.Yellow;
                    }
                    else
                    {
                        lbl_AI_eat.BackColor = Color.Silver;
                        lbl_AI_love.BackColor = Color.Silver;
                        lbl_AI_name.BackColor = Color.Silver;
                        label2.BackColor = Color.Silver;
                        panel1.BackColor = Color.Silver;
                    }
                }
                else
                {
                    lbl_AI_eat.ForeColor = Color.Green;
                    lbl_AI_eat.BackColor = Color.Silver;
                    lbl_AI_love.BackColor = Color.Silver;
                    lbl_AI_name.BackColor = Color.Silver;
                    label2.BackColor = Color.Silver;
                    panel1.BackColor = Color.Silver;
                }

                int offset3 = 0x005B3730;
                finaladdr = (IntPtr)(baseaddr + offset3);
                buffer = new byte[2];
                ok = ReadProcessMemory(curprocess.Handle, finaladdr, buffer, 2, out bytesread);
                int tmpint = System.BitConverter.ToInt16(buffer, 0);
                lbl_AI_love.Text = tmpint.ToString();
            }
            catch (Exception ex)
            { }

            timer1.Enabled = true;
            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
